package com.itheima.health.config;

import com.itheima.health.security.SercurityUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    SercurityUserDetailService userDetailService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭crsf攻击
        http.csrf().disable();
        //配置登录 登出页面
        http.formLogin()
                .loginProcessingUrl("/sec/login") // 登录请求地址  (前端异步请求需要修改)  security只能接收表单提交  不能接收json提交
                .failureForwardUrl("/user/loginFail") // 登录失败转地址 （转发）
                //.failureUrl("/login-fail.html") //重定向
                //.defaultSuccessUrl("")
                .successForwardUrl("/user/loginSuccess") // 登录成功转发地址
                //因为不在同一个模块   所以要加域名
                .loginPage("http://localhost:8080/pages/login.html") // 替换security登录页    不在同一个模块要加域名地址
                // 设置登出相关配置
                .and().logout()
                .logoutUrl("/sec/logout") // 登出请求地址
                .logoutSuccessUrl("http://localhost:8080/pages/login.html"); // 登出成功后跳转地址
    }
}
